package com.modules.system.service.impl;

import com.common.base.BaseServiceImpl;
import com.common.exception.CustomException;
import com.modules.system.dto.SysMenuDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.common.constant.Constants;
import com.modules.system.dao.SysMenuDao;
import com.modules.system.entity.SysMenuEntity;
import com.modules.system.service.SysMenuService;
import com.modules.system.service.SysUserService;

import java.util.ArrayList;
import java.util.List;


@Service("sysMenuService")
public class SysMenuServiceImpl  extends BaseServiceImpl<SysMenuEntity> implements SysMenuService {
	@Autowired
	private SysMenuDao sysMenuDao;
	@Autowired
	private SysUserService sysUserService;
	@Autowired
	private void setBaseMapper(){
		super.setBaseMapper(sysMenuDao);
	}

	//-----------------------------------------------------------------------------

	@Override
	@Transactional
	public void deleteWithAssociation(Long menuId){
		if(menuId <= Constants.SYSTEM_MAX_MENU){
			throw new CustomException("系统菜单，不能删除");
		}

		//判断是否有子菜单或按钮
		List<SysMenuDto> menuList = sysMenuDao.getListByParentId(menuId);
		if(menuList.size() > 0){
			throw new CustomException("请先删除子菜单或按钮");
		}
		sysMenuDao.deleteBatchWithAssociation(new Long[]{menuId});
	}

	@Override
	public List<SysMenuDto> getUserMenuList(Long userId) {
		//系统管理员，拥有最高权限
		if(userId == Constants.SUPER_ADMIN){
			return getContainMenuList(null);
		}

		//用户菜单列表
		List<Long> menuIdList = sysUserService.getAllMenuId(userId);
		return getContainMenuList(menuIdList);
	}


	@Override
	public List<SysMenuDto> getAllTree() {
		//查询根菜单列表
		List<SysMenuDto> menuList = getMenuByParentId(0L, null);
		//递归获取子菜单
		setChildrenMenu(menuList);
		return menuList;

	}




	/**
	 * 获取所有菜单列表
	 * @param containIdList 要包含的菜单列表，值为null返回全部，有值时不在列表中的菜单将被过滤
	 * @return
	 */
	private List<SysMenuDto> getContainMenuList(List<Long> containIdList){
		//查询子菜单
		List<SysMenuDto> menuList = getMenuByParentId(0L, containIdList);
		//获取目录下的子菜单
		setCatalogChildrenMenu(menuList, containIdList);

		return menuList;
	}


	/**
	 * 根据id查询子菜单
	 * @param parentId 父节点id
	 * @param containIdList 要包含的菜单列表，值为null返回全部，有值时不在列表中的菜单将被过滤
	 */
	private List<SysMenuDto> getMenuByParentId(Long parentId, List<Long> containIdList) {
		List<SysMenuDto> menuList = sysMenuDao.getListByParentId(parentId);
		if(containIdList == null){
			return menuList;
		}

		List<SysMenuDto> userMenuList = new ArrayList<SysMenuDto>();
		for(SysMenuDto menu : menuList){
			if(containIdList.contains(menu.getMenuId())){
				userMenuList.add(menu);
			}
		}
		return userMenuList;
	}

	/**
	 * 获取目录下的子菜单
	 * @param containIdList 要包含的菜单列表，值为null返回全部，有值时不在列表中的菜单将被过滤
	 */
	private void setCatalogChildrenMenu(List<SysMenuDto> parentMenuList, List<Long> containIdList){
		if(parentMenuList==null || parentMenuList.size()==0){
			return;
		}
		for(SysMenuDto parent : parentMenuList){
			if(parent.getType() == Constants.MenuType.CATALOG.getValue()){//目录
				parent.setChildren(getMenuByParentId(parent.getMenuId(), containIdList));
				setCatalogChildrenMenu(parent.getChildren(), containIdList);
			}
		}
		return;
	}
	
	/**
	 *  获取所有目录下的子菜单
	 */
	private void setChildrenMenu(List<SysMenuDto> menuList){
		if(menuList==null || menuList.size()==0){
			return;
		}
		for(SysMenuDto entity : menuList){
			if(entity.getType() != Constants.MenuType.BUTTON.getValue()){
				List<SysMenuDto> children = sysMenuDao.getListByParentId(entity.getMenuId());
				if(null != children && children.size()>0){
					entity.setChildren(children);
					setChildrenMenu(children);
				}

			}
		}
		return;
	}

}
